<template>
  <div class="common-layout">
    <el-container>
      <el-header>
        <el-menu
            class="el-menu-demo"
            mode="horizontal"
            :ellipsis="false">
          <el-menu-item index="/admin/index">
            <img
                style="width: 100px"
                src="@/assets/images/logo1.png"
                alt="Element logo"
            />
          </el-menu-item>
          <el-sub-menu index="1">
            <template #title>{{ admin.username }}</template>
            <el-menu-item @click="showInfoDialog">个人中心</el-menu-item>
            <el-menu-item @click="chgPwdDialogShow = true" index="chgPwd">修改密码</el-menu-item>
            <el-menu-item @click="logout" index="logout">安全退出</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu
              class="el-menu-vertical-demo"
              :router="true">

            <el-menu-item index="/admin/index">
              <el-icon>
                <HomeFilled/>
              </el-icon>
              后台首页
            </el-menu-item>
            <el-sub-menu index="1">
              <template #title>
                <el-icon>
                  <Avatar/>
                </el-icon>
                <span>管理员管理</span>
              </template>
              <el-menu-item index="/admin/admin">管理员</el-menu-item>

            </el-sub-menu>
            <el-sub-menu index="2">
              <template #title>
                <el-icon><Files /></el-icon>
                <span>商品分类管理</span>
              </template>
              <el-menu-item index="/admin/category">商品分类</el-menu-item>

            </el-sub-menu>
            <el-sub-menu index="3">
              <template #title>
                <el-icon>
                  <ShoppingCart/>
                </el-icon>
                <span>商品管理</span>
              </template>
              <el-menu-item index="/admin/goods">商品</el-menu-item>

            </el-sub-menu>
            <el-sub-menu index="4">
              <template #title>
                <el-icon>
                  <UserFilled/>
                </el-icon>
                <span>用户管理</span>
              </template>
              <el-menu-item index="/admin/user">用户</el-menu-item>

            </el-sub-menu>
            <el-sub-menu index="5">
              <template #title>
                <el-icon><Tickets /></el-icon>
                <span>订单管理</span>
              </template>
              <el-menu-item index="/admin/order">订单</el-menu-item>

            </el-sub-menu>
          </el-menu>
        </el-aside>
        <el-main>
          <RouterView/>
        </el-main>
      </el-container>
    </el-container>
  </div>
  <!-- 修改密码对话狂框 -->
  <div>
    <el-dialog v-model="chgPwdDialogShow" title="修改密码" width="500">
      <el-form :rules="rules" ref="ruleFormRef" :model="chgPwdObj">
        <el-form-item label="原密码" label-width="20%" prop="name">
          <el-input type="password" autocomplete="off" v-model="chgPwdObj.oldPwd" placeholder="请输入原密码" />
        </el-form-item>
        <el-form-item label="新密码" label-width="20%" prop="dscp">
          <el-input type="password" autocomplete="off" v-model="chgPwdObj.newPwd" placeholder="请输入新密码" />
        </el-form-item>
        <el-form-item label="新密码确认" label-width="20%" prop="dscp">
          <el-input type="password" autocomplete="off" v-model="chgPwdObj.newPwd1" placeholder="请确认新密码" />
        </el-form-item>
          <div class="dialog-footer">
            <el-button @click="chgPwdDialogShow = false">取消</el-button>
            <el-button type="primary" @click="chgPwd()">
              保存
            </el-button>
          </div>

      </el-form>
    </el-dialog>
    <!-- 修改信息对话框开始 -->
    <el-dialog v-model="infoDialog" title="管理员个人信息" width="500">
      <el-form>
        <el-form-item label="用户名" label-width="20%">
          <el-input disabled v-model="updateInfo.username"  autocomplete="off" style="width: 300px;"/>
        </el-form-item>
        <el-form-item label="电话" label-width="20%">
          <el-input v-model="updateInfo.phone"  autocomplete="off" style="width: 300px;"/>
        </el-form-item>
        <el-form-item label="邮箱" label-width="20%">
          <el-input v-model="updateInfo.email"  autocomplete="off" style="width: 300px;"/>
        </el-form-item>
        <el-form-item label="真实姓名" label-width="20%">
          <el-input v-model="updateInfo.realname"  autocomplete="off" style="width: 300px;"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="infoDialog = false">取消</el-button>
          <el-button type="primary" @click="update">提交</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 修改信息对话框结束 -->

  </div>
  <!-- 修改密码的对话框结束 -->
</template>

<script setup>

import router from "@/router/index.js";
import {HomeFilled} from "@element-plus/icons-vue";
import { useTokenStore } from '@/stores/token';
import {ref} from "vue";
import adminApi from "@/api/adminApi.js";
import {ElMessage} from "element-plus";
//修改信息对话框状态
const infoDialog = ref(false);
//导出元素
const tokenStore = useTokenStore();
//是否显示修改密码的对话框
const chgPwdDialogShow = ref(false);
//保存修改密码信息的对象
const chgPwdObj = ref({
  oldPwd: null,
  newPwd: null,
  newPwd1: null
})
//保存修改信息
const updateInfo = ref({
  username: '',
  email: '',
  phone: '',
  realname: ''
});
const logout = () => {
  // 清空token
  tokenStore.$reset();
  ElMessage({
    message: '退出成功',
    type: 'success',
    duration: 2000
  });
  // 跳转回登录页
  router.push('admin/login');
}
//管理员信息
const admin = ref({
  username:null ,
});
//获取已登录用户的信息
function getInfo() {
  adminApi.getLogin()
    .then(resp => {
      admin.value = resp.data;
    })
}
//修改密码
function chgPwd() {
  adminApi.chgPwd(chgPwdObj.value)
    .then(resp => {
        if (resp.code === 10000){
          ElMessage.success(resp.msg);
          chgPwdObj.value = {
            oldPwd: null,
            newPwd: null,
            newPwd1: null
          }
        //重置token并退出
          logout();
        }else {
          ElMessage.error(resp.msg);
        }

    });

}
getInfo();
//显示修改信息对话框
function showInfoDialog() {
  adminApi.getLogin()
      .then(resp => {
        updateInfo.value = resp.data;
        infoDialog.value = true;
      })
}
//修改信息
function update() {
  adminApi.updateInfo(updateInfo.value)
      .then(resp => {
        if (resp.code == 10000) {
          ElMessage.success(resp.msg);
          infoDialog.value = false;
        } else {
          ElMessage.error(resp.msg);
        }
      })
}
</script>

<style scoped>
.el-menu--horizontal > .el-menu-item:nth-child(1) {
  margin-right: auto;
}
.el-container,.el-menu {
  height: 100%;
}

</style>